#include "FrameCounter.h"



FrameCounter::FrameCounter() {
	oldTime = 0;
	newTime = 0;
	fps = 0;
	avgFrames = 0;
	frames = 0;
}

void FrameCounter::OnRender(SDL_Surface *surface) {

	const long alpha = 20;
	const long currTime = SDL_GetTicks();

	// Only first run
	if( oldTime == 0 ) {
		oldTime = currTime;
		return;
	}

	frames++;

	if( currTime >= oldTime + 100 ) {
		
		// One second has passed
		fps = frames * 10;
		oldTime = currTime;
		frames = 0;

		if( avgFrames == 0 ) {
			avgFrames = fps;
		}
		else {
			avgFrames = ((avgFrames * (alpha-1)) + fps) / alpha;
		}
	}

	

}

long FrameCounter::GetAvgNoOfFrames() {

	return avgFrames;
}